#pragma once

#include "viml/types.h"
#include "viml/instruction.h"
#include "viml/value_fwd.h"

namespace viml {

/**
 * 函数对象
 */
class Function {
public:
    std::string name;
    std::vector<std::string> parameters;
    std::shared_ptr<Bytecode> bytecode;
    size_t start_address;  // 函数字节码起始地址
    
    Function(const std::string& n, 
             std::vector<std::string> params,
             std::shared_ptr<Bytecode> code,
             size_t start_addr = 0);
    
    size_t arity() const;
};

/**
 * 闭包对象
 */
class Closure {
public:
    std::shared_ptr<Function> function;
    std::unordered_map<std::string, Value> captured_variables;
    
    Closure(std::shared_ptr<Function> func);
    
    void capture(const std::string& name, const Value& value);
    bool has_captured(const std::string& name) const;
    Value get_captured(const std::string& name) const;
};

} // namespace viml